#include"ClientSocket.h"
#include<QHostAddress>

ClientSocket::ClientSocket(QObject *parent)
         : QTcpSocket(parent){
    connect(this,SIGNAL(readyRead()),this,SLOT(slotdataReceive()));
}

ClientSocket::~ClientSocket(){
}

void ClientSocket::slotsendMsg(QString str){
    if(!str.isEmpty()){
        QByteArray array = str.toLatin1();
        write(array);
    }
}

void ClientSocket::slotdataReceive(){
    QByteArray array1 = read(1);
    QByteArray array = read(bytesAvailable());
    QString str(array.data());
    if(array1.data()[0] == '1'){   //instant Msg
        emit updateClient(str);
    }
    emit updateHistory(str);
}

void ClientSocket::slotconnectServer(QString serverip, QString port){
    QHostAddress addr(serverip);
    connectToHost(addr,port.toInt());
}

void ClientSocket::slotdisconnectServer(){
    disconnectFromHost();
}
